package sealedGenerator;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import sealedGenerator.card.Card;

public class MWSDeckWriter {
	public static void write(File f, List<Card> cards) {
		try {
			BufferedWriter writer = new BufferedWriter(new FileWriter(f));
			writer.write("// Deck file for Magic Workstation\r\n");
			writer.write("\r\n");
			
			writer.write("// Lands\r\n");
			Map<Card, Integer> cardsHash = new HashMap<Card, Integer>();
			for(Card card: cards) {
				if("Land".equals(card.getType())) {
					if(!cardsHash.containsKey(card))
						cardsHash.put(card, 0);
					
					cardsHash.put(card, cardsHash.get(card) + 1);
				}
			}
			
			for(Card card: cardsHash.keySet()) {
				writer.write(card.getMWSDeckString(cardsHash.get(card)));
			}
			writer.write("\r\n");
			
			
			
			
			cardsHash = new HashMap<Card, Integer>();
			for(Card card: cards) {
				if("Creature".equals(card.getType())) {
					if(!cardsHash.containsKey(card))
						cardsHash.put(card, 0);
					
					cardsHash.put(card, cardsHash.get(card) + 1);
				}
			}
			
			writer.write("// Creatures\r\n");
			for(Card card: cardsHash.keySet()) {
				writer.write(card.getMWSDeckString(cardsHash.get(card)));
			}
			writer.write("\r\n");
			
			
			
			cardsHash = new HashMap<Card, Integer>();
			for(Card card: cards) {
				if(!"Creature".equals(card.getType()) && !"Land".equals(card.getType())) {
					if(!cardsHash.containsKey(card))
						cardsHash.put(card, 0);
					
					cardsHash.put(card, cardsHash.get(card) + 1);
				}
			}
			
			writer.write("// Spells\r\n");
			for(Card card: cardsHash.keySet()) {
				writer.write(card.getMWSDeckString(cardsHash.get(card)));
			}
			writer.write("\r\n");
			
			writer.close();
		}
		catch(Exception e) {
			e.printStackTrace();
		}
	}
}
